package com.rockcent.service.impl;

import com.rockcent.common.common.PageResult;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.SysMessage;
import com.rockcent.mall.lib.domain.SysMessageUser;
import com.rockcent.mall.lib.domain.enumclass.SysMessageStatus;
import com.rockcent.mall.lib.dto.SysMessageDto;
import com.rockcent.mall.lib.repository.SysMessageRepository;
import com.rockcent.mall.lib.repository.SysMessageUserRepository;
import com.rockcent.service.SysMessageSrv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by zhangjh on 2017/5/27.
 */
@Service
public class SysMessageSrvImpl implements SysMessageSrv {

    @Autowired
    private SysMessageRepository sysMessageRepository;

    @Autowired
    private SysMessageUserRepository sysMessageUserRepository;

    @Override
    public SysMessage saveSysMessage(SysMessage message) {
        message = sysMessageRepository.save(message);
        return message;
    }

    @Override
    public SysMessageDto findOneByIdAndIsDeleteAndUserId(Long id, Whether no, Long userId) {
        return sysMessageRepository.findOneByIdAndIsDeleteAndUserId(id, no, userId);
    }

    @Override
    public void readMsg(Long id, Long userId) {
        sysMessageUserRepository.updateStatusByIdAndUserId(SysMessageStatus.READED.toString(), id, userId);
    }


    @Override
    public PageResult<SysMessageDto> page(SysMessageDto message, PageResult page) {
        return sysMessageRepository.querySysMessage(message, page);
    }

    @Override
    public SysMessageUser saveSysMessageUser(SysMessageUser sysMessageUser) {
        return sysMessageUserRepository.save(sysMessageUser);
    }

    @Override
    public SysMessageUser findOneById(Long id, Whether no) {
        return sysMessageUserRepository.findOne(id);
    }
}
